package com.y2.authprj.mvc.controller.auth;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.gdglc.spring.biz.auth.FunctionauthorityBiz;
import com.github.pagehelper.PageInfo;
import com.y2.authprj.bean.auth.model.FunctionauthorityInfo;
import com.y2.authprj.bean.auth.model.StaffInfo;
import com.y2.authprj.dao.auth.IRoleauthorityDao;
import com.y2.authprj.exception.BizException;
import com.y2.authprj.utils.login.LoginCheck;

@Controller
@RequestMapping("/auth/functionauthority")
public class FunctionauthorityController {

	@Resource(name="FunctionauthorityBiz")
	private FunctionauthorityBiz biz;

	public FunctionauthorityBiz getBiz() {
		return biz;
	}

	public void setBiz(FunctionauthorityBiz biz) {
		this.biz = biz;
	}
	@LoginCheck("查询资源")
	@RequestMapping("/list")
	public String list(Model model,Integer parentId,@RequestParam(
			required=false,defaultValue = "1")int pageNum){
		try {
			List<FunctionauthorityInfo> functionauthorityList = biz.findAll();
			model.addAttribute("functionauthorityList", functionauthorityList);
			if(parentId==null || parentId==0){
				PageInfo<FunctionauthorityInfo> queryByPage = biz.findAllByName("", pageNum, 5);
				List<FunctionauthorityInfo>  findAllByName= queryByPage.getList();
				model.addAttribute("queryByPage",queryByPage);
				model.addAttribute("fByParent", findAllByName);
				
			}else{
				PageInfo<FunctionauthorityInfo> queryByPage=biz.findAllByParentId(parentId, pageNum, 5);
				List<FunctionauthorityInfo>  fByParent= queryByPage.getList();
				model.addAttribute("queryByPage",queryByPage);
				model.addAttribute("fByParent", fByParent);
				model.addAttribute("parentId", parentId);
			}
			
		} catch (Exception e) {
			e.printStackTrace();
		}
		return "/auth/functionauthority/functionauthority";
	}
	
	@RequestMapping("/showParent")
	public String showParent(Model model,Integer parentId,@RequestParam(
			required=false,defaultValue = "1")int pageNum,HttpSession session){
		List<FunctionauthorityInfo> fByParent=null;
		try {
			//fByParent= biz.findAllByParentId(parentId);
			//
			if(session.getAttribute("maxPage")!=null){
				int max=(int) session.getAttribute("maxPage");
				
				if(pageNum>max){
					pageNum=max;
				}
			}
			List<FunctionauthorityInfo> functionauthorityList = biz.findAll();
			
			PageInfo<FunctionauthorityInfo> queryByPage = biz.findAllByParentId(parentId, pageNum, 5);
			fByParent = queryByPage.getList();
			
			int maxPage=queryByPage.getPages();
			
			session.setAttribute("maxPage", maxPage);
			model.addAttribute("queryByPage",queryByPage);
			model.addAttribute("functionauthorityList", functionauthorityList);
			model.addAttribute("fByParent", fByParent);
			model.addAttribute("parentId", parentId);
			model.addAttribute("pageNum", pageNum);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return "/auth/functionauthority/functionauthority";
		
		
	}
	@LoginCheck("添加资源")
	@RequestMapping("/add")
	@ResponseBody
	public String add(FunctionauthorityInfo finfo,Model model){
		if(biz.findAllByParentIdAndName(finfo)==0){
			try {
				biz.add(finfo);
			} catch (BizException e) {
				model.addAttribute("errorMessage", e.getMessage());
			}
			return "true";
		}else{
			return "false";
		}
			
				
	}
	
	//
	@RequestMapping("/addCheckByName")
	@ResponseBody
	public String addCheckByName(String addName,Integer parentId,String addkeyValue,Model model){
			FunctionauthorityInfo info=new FunctionauthorityInfo();
			info.setName(addName);
			info.setParentId(parentId);
			info.setKeyValue(addkeyValue);
			int i= biz.findAllByParentIdAndName(info);
			if(i>0){
				
				return "true";
			}else{
				return "false";
			}
		
	}
	@LoginCheck("删除资源")
	@RequestMapping("/del")
	@ResponseBody
	public String del(String[] delitems,Model model){
		String msg="";
		for (int i = 0; i < delitems.length; i++) {
			try {
				if(biz.findAllByParentId(Integer.parseInt(delitems[i])).size()==0){
					biz.del(Integer.parseInt(delitems[i]));
					msg="true";
				}else{
					msg="false";
					
				}
				
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		
		return msg;
	}
	@RequestMapping("/findByName")
	public String findByName(Model model,@RequestParam(
			required=false,defaultValue = "1")int pageNum,
			String name){
		List<FunctionauthorityInfo> functionauthorityList = null;
		try {
			functionauthorityList = biz.findAll();
		} catch (BizException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		model.addAttribute("functionauthorityList", functionauthorityList);
		//List<FunctionauthorityInfo> findAllByName = biz.findAllByName(name);
		try {
			biz.findAllByName(name);
			
		} catch (BizException e) {
			model.addAttribute("msg", e.getMessage());
		}
		PageInfo<FunctionauthorityInfo> queryByPage = biz.findAllByName(name, pageNum, 5);
		
		List<FunctionauthorityInfo>  findAllByName= queryByPage.getList();
		model.addAttribute("queryByPage",queryByPage);
		model.addAttribute("fByParent", findAllByName);
		model.addAttribute("name", name);
		model.addAttribute("pageNum", pageNum);
		return "/auth/functionauthority/functionauthority";
		
	}
	
	
	
}
